//
//  ERClassType.m
//  metadata
//
//  Created by easy on 11-5-30.
//  Copyright 2011 zeasy@qq.com. All rights reserved.
//

#import "ERClassType.h"

#import "ERClass.h"

@implementation ERClassType
@synthesize type;
@synthesize size;
@synthesize alignment;

-(id) initWithType:(NSString *) aType{
	if (self = [super init]) {
		type = [aType retain];
		
		@try {
			NSGetSizeAndAlignment(ER_CSTRING(self.type), &size, &alignment);
		}
		@catch (NSException * e) {
			//NSLog(@"%@",e);
		}
		@finally {
		}
			
		
	}
	return self;
}
+(id) typeWithEncode:(const char *)encode{
	return [ERClassType typeWithCString:encode];
}

+(id) typeWithCString:(const char *)cType{
	NSString *aType = ER_STRING_ALLOC(cType);
	ERClassType *classType = [[ERClassType alloc] initWithType:aType];
	[aType release];
	return [classType autorelease];
}
+(id) erClassType:(ERClass *)aClazz{
	return [[[ERClassType alloc] initWithType:aClazz.name] autorelease];
}
+(id) classType:(Class) aClazz{
	NSMutableString *typeName = [[NSMutableString alloc] initWithString:@"@\""];
	[typeName appendString:NSStringFromClass(aClazz)];
	[typeName appendString:@"\""];
	ERClassType *classType = [[ERClassType alloc] initWithType:typeName];
	[typeName release];
	return [classType autorelease];
}
+(id) intClassType{
	static ERClassType *intType = nil;
	@synchronized(self){
		if (!intType) {
			intType = [ERClassType typeWithEncode:@encode(int)];
		}
	}
	return intType;
}
+(id) boolClassType{
	static ERClassType *boolType = nil;
	@synchronized(self){
		if (!boolType) {
			boolType = [ERClassType typeWithEncode:@encode(BOOL)];
		}
	}
	return boolType;
}
+(id) charClassType{
	static ERClassType *charType = nil;
	@synchronized(self){
		if (!charType) {
			charType = [ERClassType typeWithEncode:@encode(char)];
		}
	}
	return charType;
}
+(id) shortClassType{
	static ERClassType *shortType = nil;
	@synchronized(self){
		if (!shortType) {
			shortType = [ERClassType typeWithEncode:@encode(short)];
		}
	}
	return shortType;
}
+(id) longClassType{
	static ERClassType *longType = nil;
	@synchronized(self){
		if (!longType) {
			longType = [ERClassType typeWithEncode:@encode(long)];
		}
	}
	return longType;
}
+(id) floatClassType{
	static ERClassType *floatType = nil;
	@synchronized(self){
		if (!floatType) {
			floatType = [ERClassType typeWithEncode:@encode(float)];
		}
	}
	return floatType;
}
+(id) doubleClassType{
	static ERClassType *doubleType = nil;
	@synchronized(self){
		if (!doubleType) {
			doubleType = [ERClassType typeWithEncode:@encode(double)];
		}
	}
	return doubleType;
}
-(BOOL) isObject{
	return [self.type length] && [self.type characterAtIndex:0] == '@';
}
-(BOOL) isEncode:(const char *)encode{
	return [self.type length]&& [self.type isEqual:[ER_STRING_ALLOC(encode) autorelease]];
}
-(BOOL) isInt{
	return [self isEncode:@encode(int)];
}
-(BOOL) isBool{
	return [self isEncode:@encode(BOOL)];
}
-(BOOL) isChar{
	return [self isEncode:@encode(char)];
}
-(BOOL) isShort{
	return [self isEncode:@encode(short)];
}
-(BOOL) isLong{
	return [self isEncode:@encode(long)];
}
-(BOOL) isFloat{
	return [self isEncode:@encode(float)];
}
-(BOOL) isDouble{
	return [self isEncode:@encode(double)];
}
-(BOOL) isVoid{
	return [self isEncode:@encode(void)];
}
-(BOOL) isPointer{
	return [self.type length] && [self.type characterAtIndex:0] == '^';
}
-(BOOL) isUnkown{
	return [self.type length] && [self.type characterAtIndex:0] == '?';
}
-(void) dealloc{
	[type release];
	[super dealloc];
}
@end
